#include "GameListXmlHandler.h"

#include <QDebug>

GameListXmlHandler::GameListXmlHandler()
{
}

bool GameListXmlHandler::characters(const QString &str)
{
    m_currentText += str;
    return true;
}

bool GameListXmlHandler::fatalError(const QXmlParseException &exception)
{
    qDebug("Parse error at line %d, column %d:\n%s", exception.lineNumber(),
                    exception.columnNumber(), exception.message().toStdString().c_str() );

    return false;
}

bool GameListXmlHandler::startElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
                  const QString &qName, const QXmlAttributes &/*attributes*/)
{
    //qDebug() << "Start: " << qName;

    if (qName == "game")
        m_currentGame = Game();


    m_currentText.clear();
    return true;

}

bool GameListXmlHandler::endElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
                const QString &qName)
{
    if (qName == "name")
        m_currentGame.setName(m_currentText);
    else if (qName == "logo")
        m_currentGame.setLogoUrl(m_currentText);
    else if (qName == "statsLink")
        m_currentGame.setStatsUrl(m_currentText);
    else if (qName == "storeLink")
        m_currentGame.setStoreUrl(m_currentText);
    else if (qName == "game")
    {
        m_gameMap.insert(m_currentGame.name(), m_currentGame);
    }

    //qDebug() << "End: " << qName << ": " << m_currentText;

    return true;
}

QMap<QString, Game> GameListXmlHandler::games() const
{
    return m_gameMap;
}
